Password generator code

import random import string def generate_password(length=12): if length < 4: raise ValueError("Password length should be at least 4 characters.") # Character sets letters = string.ascii_letters # a-zA-Z digits = string.digits # 0-9 symbols = string.punctuation # !@#$%^&*() etc. # Ensure the password has at least one letter, digit, and symbol password = [ random.choice(letters), random.choice(digits), random.choice(symbols) ] # Fill the rest with random characters from all sets all_chars = letters + digits + symbols password += random.choices(all_chars, k=length - 3) # Shuffle for randomness random.shuffle(password) return ''.join(password) # Example usage: print("Generated password:", generate_password(12))

Code output

Generated password: G9#a2k&Mq!dL